const path = require('path');
const fs = require('fs-extra');
const Inquirer = require('inquirer');
const Creator = require('./Creator');

module.exports = async function (projectName,opts) {
    // console.log(projectName,opts);
    
    const cwd = process.cwd(); // 获取当前命令执行时的工作目录
    const targetDir = path.join(cwd,projectName); // 目标目录
    async function overWrite() {
         // 先移除
        await fs.remove(targetDir)
        new Creator(projectName,targetDir);
    }
    // 首先判断项目是否已经存在
    if(fs.existsSync(targetDir)){
        // 继而判断是否是强制覆盖模式
        if(opts.force){
           await overWrite()
        }else {
            // 提示用户是否确定覆盖
            let {action} = await Inquirer.prompt([
                {
                    name: 'action',
                    type: 'list', // 还有很多其他类型
                    message: 'Target directory already exited Pick an action',
                    choices: [
                        {
                            name: 'Overwrite', value: 'overwrite'
                        },
                        {name: 'cancel', value: false}
                    ]
                }
            ]);

            // // console.log(action);
            if(!action){
                return
            }else if(action === 'overwrite') {
                await overWrite()
            }
        }
    }else {
        const creator = new Creator(projectName,targetDir);
        creator.create()
    }
}